<?php


abstract class BasePeerPeer {

	
	const DATABASE_NAME = 'peergate';

	
	const TABLE_NAME = 'peers';

	
	const CLASS_DEFAULT = 'lib.model.Peer';

	
	const NUM_COLUMNS = 26;

	
	const NUM_LAZY_LOAD_COLUMNS = 0;


	
	const ID = 'peers.ID';

	
	const INFO_HASH = 'peers.INFO_HASH';

	
	const IP4_1 = 'peers.IP4_1';

	
	const IP4_2 = 'peers.IP4_2';

	
	const IP4_3 = 'peers.IP4_3';

	
	const IP4_4 = 'peers.IP4_4';

	
	const IP4_PORT = 'peers.IP4_PORT';

	
	const IP6_1 = 'peers.IP6_1';

	
	const IP6_2 = 'peers.IP6_2';

	
	const IP6_3 = 'peers.IP6_3';

	
	const IP6_4 = 'peers.IP6_4';

	
	const IP6_5 = 'peers.IP6_5';

	
	const IP6_6 = 'peers.IP6_6';

	
	const IP6_7 = 'peers.IP6_7';

	
	const IP6_8 = 'peers.IP6_8';

	
	const IP6_PORT = 'peers.IP6_PORT';

	
	const ONION = 'peers.ONION';

	
	const STATS = 'peers.STATS';

	
	const BYTES_UPLOADED = 'peers.BYTES_UPLOADED';

	
	const BYTES_UPLOAD_OFFSET = 'peers.BYTES_UPLOAD_OFFSET';

	
	const BYTES_DOWNLOAD_OFFSET = 'peers.BYTES_DOWNLOAD_OFFSET';

	
	const BYTES_LEFT = 'peers.BYTES_LEFT';

	
	const AGENT = 'peers.AGENT';

	
	const CREATED_AT = 'peers.CREATED_AT';

	
	const UPDATED_AT = 'peers.UPDATED_AT';

	
	const FINISHED_AT = 'peers.FINISHED_AT';

	
	private static $phpNameMap = null;


	
	private static $fieldNames = array (
		BasePeer::TYPE_PHPNAME => array ('Id', 'InfoHash', 'Ip41', 'Ip42', 'Ip43', 'Ip44', 'Ip4Port', 'Ip61', 'Ip62', 'Ip63', 'Ip64', 'Ip65', 'Ip66', 'Ip67', 'Ip68', 'Ip6Port', 'Onion', 'Stats', 'BytesUploaded', 'BytesUploadOffset', 'BytesDownloadOffset', 'BytesLeft', 'Agent', 'CreatedAt', 'UpdatedAt', 'FinishedAt', ),
		BasePeer::TYPE_COLNAME => array (PeerPeer::ID, PeerPeer::INFO_HASH, PeerPeer::IP4_1, PeerPeer::IP4_2, PeerPeer::IP4_3, PeerPeer::IP4_4, PeerPeer::IP4_PORT, PeerPeer::IP6_1, PeerPeer::IP6_2, PeerPeer::IP6_3, PeerPeer::IP6_4, PeerPeer::IP6_5, PeerPeer::IP6_6, PeerPeer::IP6_7, PeerPeer::IP6_8, PeerPeer::IP6_PORT, PeerPeer::ONION, PeerPeer::STATS, PeerPeer::BYTES_UPLOADED, PeerPeer::BYTES_UPLOAD_OFFSET, PeerPeer::BYTES_DOWNLOAD_OFFSET, PeerPeer::BYTES_LEFT, PeerPeer::AGENT, PeerPeer::CREATED_AT, PeerPeer::UPDATED_AT, PeerPeer::FINISHED_AT, ),
		BasePeer::TYPE_FIELDNAME => array ('id', 'info_hash', 'ip4_1', 'ip4_2', 'ip4_3', 'ip4_4', 'ip4_port', 'ip6_1', 'ip6_2', 'ip6_3', 'ip6_4', 'ip6_5', 'ip6_6', 'ip6_7', 'ip6_8', 'ip6_port', 'onion', 'stats', 'bytes_uploaded', 'bytes_upload_offset', 'bytes_download_offset', 'bytes_left', 'agent', 'created_at', 'updated_at', 'finished_at', ),
		BasePeer::TYPE_NUM => array (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, )
	);

	
	private static $fieldKeys = array (
		BasePeer::TYPE_PHPNAME => array ('Id' => 0, 'InfoHash' => 1, 'Ip41' => 2, 'Ip42' => 3, 'Ip43' => 4, 'Ip44' => 5, 'Ip4Port' => 6, 'Ip61' => 7, 'Ip62' => 8, 'Ip63' => 9, 'Ip64' => 10, 'Ip65' => 11, 'Ip66' => 12, 'Ip67' => 13, 'Ip68' => 14, 'Ip6Port' => 15, 'Onion' => 16, 'Stats' => 17, 'BytesUploaded' => 18, 'BytesUploadOffset' => 19, 'BytesDownloadOffset' => 20, 'BytesLeft' => 21, 'Agent' => 22, 'CreatedAt' => 23, 'UpdatedAt' => 24, 'FinishedAt' => 25, ),
		BasePeer::TYPE_COLNAME => array (PeerPeer::ID => 0, PeerPeer::INFO_HASH => 1, PeerPeer::IP4_1 => 2, PeerPeer::IP4_2 => 3, PeerPeer::IP4_3 => 4, PeerPeer::IP4_4 => 5, PeerPeer::IP4_PORT => 6, PeerPeer::IP6_1 => 7, PeerPeer::IP6_2 => 8, PeerPeer::IP6_3 => 9, PeerPeer::IP6_4 => 10, PeerPeer::IP6_5 => 11, PeerPeer::IP6_6 => 12, PeerPeer::IP6_7 => 13, PeerPeer::IP6_8 => 14, PeerPeer::IP6_PORT => 15, PeerPeer::ONION => 16, PeerPeer::STATS => 17, PeerPeer::BYTES_UPLOADED => 18, PeerPeer::BYTES_UPLOAD_OFFSET => 19, PeerPeer::BYTES_DOWNLOAD_OFFSET => 20, PeerPeer::BYTES_LEFT => 21, PeerPeer::AGENT => 22, PeerPeer::CREATED_AT => 23, PeerPeer::UPDATED_AT => 24, PeerPeer::FINISHED_AT => 25, ),
		BasePeer::TYPE_FIELDNAME => array ('id' => 0, 'info_hash' => 1, 'ip4_1' => 2, 'ip4_2' => 3, 'ip4_3' => 4, 'ip4_4' => 5, 'ip4_port' => 6, 'ip6_1' => 7, 'ip6_2' => 8, 'ip6_3' => 9, 'ip6_4' => 10, 'ip6_5' => 11, 'ip6_6' => 12, 'ip6_7' => 13, 'ip6_8' => 14, 'ip6_port' => 15, 'onion' => 16, 'stats' => 17, 'bytes_uploaded' => 18, 'bytes_upload_offset' => 19, 'bytes_download_offset' => 20, 'bytes_left' => 21, 'agent' => 22, 'created_at' => 23, 'updated_at' => 24, 'finished_at' => 25, ),
		BasePeer::TYPE_NUM => array (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, )
	);

	
	public static function getMapBuilder()
	{
		return BasePeer::getMapBuilder('lib.model.map.PeerMapBuilder');
	}
	
	public static function getPhpNameMap()
	{
		if (self::$phpNameMap === null) {
			$map = PeerPeer::getTableMap();
			$columns = $map->getColumns();
			$nameMap = array();
			foreach ($columns as $column) {
				$nameMap[$column->getPhpName()] = $column->getColumnName();
			}
			self::$phpNameMap = $nameMap;
		}
		return self::$phpNameMap;
	}
	
	static public function translateFieldName($name, $fromType, $toType)
	{
		$toNames = self::getFieldNames($toType);
		$key = isset(self::$fieldKeys[$fromType][$name]) ? self::$fieldKeys[$fromType][$name] : null;
		if ($key === null) {
			throw new PropelException("'$name' could not be found in the field names of type '$fromType'. These are: " . print_r(self::$fieldKeys[$fromType], true));
		}
		return $toNames[$key];
	}

	

	static public function getFieldNames($type = BasePeer::TYPE_PHPNAME)
	{
		if (!array_key_exists($type, self::$fieldNames)) {
			throw new PropelException('Method getFieldNames() expects the parameter $type to be one of the class constants TYPE_PHPNAME, TYPE_COLNAME, TYPE_FIELDNAME, TYPE_NUM. ' . $type . ' was given.');
		}
		return self::$fieldNames[$type];
	}

	
	public static function alias($alias, $column)
	{
		return str_replace(PeerPeer::TABLE_NAME.'.', $alias.'.', $column);
	}

	
	public static function addSelectColumns(Criteria $criteria)
	{

		$criteria->addSelectColumn(PeerPeer::ID);

		$criteria->addSelectColumn(PeerPeer::INFO_HASH);

		$criteria->addSelectColumn(PeerPeer::IP4_1);

		$criteria->addSelectColumn(PeerPeer::IP4_2);

		$criteria->addSelectColumn(PeerPeer::IP4_3);

		$criteria->addSelectColumn(PeerPeer::IP4_4);

		$criteria->addSelectColumn(PeerPeer::IP4_PORT);

		$criteria->addSelectColumn(PeerPeer::IP6_1);

		$criteria->addSelectColumn(PeerPeer::IP6_2);

		$criteria->addSelectColumn(PeerPeer::IP6_3);

		$criteria->addSelectColumn(PeerPeer::IP6_4);

		$criteria->addSelectColumn(PeerPeer::IP6_5);

		$criteria->addSelectColumn(PeerPeer::IP6_6);

		$criteria->addSelectColumn(PeerPeer::IP6_7);

		$criteria->addSelectColumn(PeerPeer::IP6_8);

		$criteria->addSelectColumn(PeerPeer::IP6_PORT);

		$criteria->addSelectColumn(PeerPeer::ONION);

		$criteria->addSelectColumn(PeerPeer::STATS);

		$criteria->addSelectColumn(PeerPeer::BYTES_UPLOADED);

		$criteria->addSelectColumn(PeerPeer::BYTES_UPLOAD_OFFSET);

		$criteria->addSelectColumn(PeerPeer::BYTES_DOWNLOAD_OFFSET);

		$criteria->addSelectColumn(PeerPeer::BYTES_LEFT);

		$criteria->addSelectColumn(PeerPeer::AGENT);

		$criteria->addSelectColumn(PeerPeer::CREATED_AT);

		$criteria->addSelectColumn(PeerPeer::UPDATED_AT);

		$criteria->addSelectColumn(PeerPeer::FINISHED_AT);

	}

	const COUNT = 'COUNT(peers.ID)';
	const COUNT_DISTINCT = 'COUNT(DISTINCT peers.ID)';

	
	public static function doCount(Criteria $criteria, $distinct = false, $con = null)
	{
				$criteria = clone $criteria;

				$criteria->clearSelectColumns()->clearOrderByColumns();
		if ($distinct || in_array(Criteria::DISTINCT, $criteria->getSelectModifiers())) {
			$criteria->addSelectColumn(PeerPeer::COUNT_DISTINCT);
		} else {
			$criteria->addSelectColumn(PeerPeer::COUNT);
		}

				foreach($criteria->getGroupByColumns() as $column)
		{
			$criteria->addSelectColumn($column);
		}

		$rs = PeerPeer::doSelectRS($criteria, $con);
		if ($rs->next()) {
			return $rs->getInt(1);
		} else {
						return 0;
		}
	}
	
	public static function doSelectOne(Criteria $criteria, $con = null)
	{
		$critcopy = clone $criteria;
		$critcopy->setLimit(1);
		$objects = PeerPeer::doSelect($critcopy, $con);
		if ($objects) {
			return $objects[0];
		}
		return null;
	}
	
	public static function doSelect(Criteria $criteria, $con = null)
	{
		return PeerPeer::populateObjects(PeerPeer::doSelectRS($criteria, $con));
	}
	
	public static function doSelectRS(Criteria $criteria, $con = null)
	{
		if ($con === null) {
			$con = Propel::getConnection(self::DATABASE_NAME);
		}

		if (!$criteria->getSelectColumns()) {
			$criteria = clone $criteria;
			PeerPeer::addSelectColumns($criteria);
		}

				$criteria->setDbName(self::DATABASE_NAME);

						return BasePeer::doSelect($criteria, $con);
	}
	
	public static function populateObjects(ResultSet $rs)
	{
		$results = array();
	
				$cls = PeerPeer::getOMClass();
		$cls = Propel::import($cls);
				while($rs->next()) {
		
			$obj = new $cls();
			$obj->hydrate($rs);
			$results[] = $obj;
			
		}
		return $results;
	}
	
	public static function getTableMap()
	{
		return Propel::getDatabaseMap(self::DATABASE_NAME)->getTable(self::TABLE_NAME);
	}

	
	public static function getOMClass()
	{
		return PeerPeer::CLASS_DEFAULT;
	}

	
	public static function doInsert($values, $con = null)
	{
		if ($con === null) {
			$con = Propel::getConnection(self::DATABASE_NAME);
		}

		if ($values instanceof Criteria) {
			$criteria = clone $values; 		} else {
			$criteria = $values->buildCriteria(); 		}


				$criteria->setDbName(self::DATABASE_NAME);

		try {
									$con->begin();
			$pk = BasePeer::doInsert($criteria, $con);
			$con->commit();
		} catch(PropelException $e) {
			$con->rollback();
			throw $e;
		}

		return $pk;
	}

	
	public static function doUpdate($values, $con = null)
	{
		if ($con === null) {
			$con = Propel::getConnection(self::DATABASE_NAME);
		}

		$selectCriteria = new Criteria(self::DATABASE_NAME);

		if ($values instanceof Criteria) {
			$criteria = clone $values; 
			$comparison = $criteria->getComparison(PeerPeer::ID);
			$selectCriteria->add(PeerPeer::ID, $criteria->remove(PeerPeer::ID), $comparison);

			$comparison = $criteria->getComparison(PeerPeer::INFO_HASH);
			$selectCriteria->add(PeerPeer::INFO_HASH, $criteria->remove(PeerPeer::INFO_HASH), $comparison);

		} else { 			$criteria = $values->buildCriteria(); 			$selectCriteria = $values->buildPkeyCriteria(); 		}

				$criteria->setDbName(self::DATABASE_NAME);

		return BasePeer::doUpdate($selectCriteria, $criteria, $con);
	}

	
	public static function doDeleteAll($con = null)
	{
		if ($con === null) {
			$con = Propel::getConnection(self::DATABASE_NAME);
		}
		$affectedRows = 0; 		try {
									$con->begin();
			$affectedRows += BasePeer::doDeleteAll(PeerPeer::TABLE_NAME, $con);
			$con->commit();
			return $affectedRows;
		} catch (PropelException $e) {
			$con->rollback();
			throw $e;
		}
	}

	
	 public static function doDelete($values, $con = null)
	 {
		if ($con === null) {
			$con = Propel::getConnection(PeerPeer::DATABASE_NAME);
		}

		if ($values instanceof Criteria) {
			$criteria = clone $values; 		} elseif ($values instanceof Peer) {

			$criteria = $values->buildPkeyCriteria();
		} else {
						$criteria = new Criteria(self::DATABASE_NAME);
												if(count($values) == count($values, COUNT_RECURSIVE))
			{
								$values = array($values);
			}
			$vals = array();
			foreach($values as $value)
			{

				$vals[0][] = $value[0];
				$vals[1][] = $value[1];
			}

			$criteria->add(PeerPeer::ID, $vals[0], Criteria::IN);
			$criteria->add(PeerPeer::INFO_HASH, $vals[1], Criteria::IN);
		}

				$criteria->setDbName(self::DATABASE_NAME);

		$affectedRows = 0; 
		try {
									$con->begin();
			
			$affectedRows += BasePeer::doDelete($criteria, $con);
			$con->commit();
			return $affectedRows;
		} catch (PropelException $e) {
			$con->rollback();
			throw $e;
		}
	}

	
	public static function doValidate(Peer $obj, $cols = null)
	{
		$columns = array();

		if ($cols) {
			$dbMap = Propel::getDatabaseMap(PeerPeer::DATABASE_NAME);
			$tableMap = $dbMap->getTable(PeerPeer::TABLE_NAME);

			if (! is_array($cols)) {
				$cols = array($cols);
			}

			foreach($cols as $colName) {
				if ($tableMap->containsColumn($colName)) {
					$get = 'get' . $tableMap->getColumn($colName)->getPhpName();
					$columns[$colName] = $obj->$get();
				}
			}
		} else {

		}

		$res =  BasePeer::doValidate(PeerPeer::DATABASE_NAME, PeerPeer::TABLE_NAME, $columns);
    if ($res !== true) {
        $request = sfContext::getInstance()->getRequest();
        foreach ($res as $failed) {
            $col = PeerPeer::translateFieldname($failed->getColumn(), BasePeer::TYPE_COLNAME, BasePeer::TYPE_PHPNAME);
            $request->setError($col, $failed->getMessage());
        }
    }

    return $res;
	}

	
	public static function retrieveByPK( $id, $info_hash, $con = null) {
		if ($con === null) {
			$con = Propel::getConnection(self::DATABASE_NAME);
		}
		$criteria = new Criteria();
		$criteria->add(PeerPeer::ID, $id);
		$criteria->add(PeerPeer::INFO_HASH, $info_hash);
		$v = PeerPeer::doSelect($criteria, $con);

		return !empty($v) ? $v[0] : null;
	}
} 
if (Propel::isInit()) {
			try {
		BasePeerPeer::getMapBuilder();
	} catch (Exception $e) {
		Propel::log('Could not initialize Peer: ' . $e->getMessage(), Propel::LOG_ERR);
	}
} else {
			Propel::registerMapBuilder('lib.model.map.PeerMapBuilder');
}
